package com.hegongda.dao.impl;

import com.hegongda.bean.Introduce;
import com.hegongda.bean.Nav;
import com.hegongda.bean.PageBean;
import com.hegongda.bean.Product;
import com.hegongda.dao.ProductDao;
import com.hegongda.servlet.Constr;
import com.hegongda.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> getHost() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql= "SELECT * FROM product where pflag = ? and is_hot = ? and pflag=0 limit ?,?";
        return qr.query(sql,new BeanListHandler<Product>(Product.class), Constr.PFLAG,Constr.ISHOT,0,9);
    }

    @Override
    public List<Product> getNews() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql= "SELECT * FROM product WHERE pflag = ? and pflag=0 ORDER BY pdate DESC LIMIT ?,?";
        return qr.query(sql,new BeanListHandler<Product>(Product.class), Constr.PFLAG,0,9);
    }

    @Override
    public List<Nav> getAllNav() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql= "SELECT * FROM category";
        return qr.query(sql,new BeanListHandler<Nav>(Nav.class));
    }

    @Override
    public Product getOneProduct(String pid) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from product where pid = ? and pflag=0";
        return qr.query(sql, new BeanHandler<Product>(Product.class), pid);
    }

    @Override
    public List<Product> getPageProduct(String cid,int fistnum, int lastnum) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql= "SELECT * FROM product WHERE cid = ? and pflag=0 LIMIT ?,?";
        return qr.query(sql,new BeanListHandler<Product>(Product.class),cid,fistnum,lastnum);
    }

    @Override
    public long getProductCount(String cid) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select count(*) from product where cid=? and pflag=0";
        return (Long)qr.query(sql,new ScalarHandler(),cid);
    }

    @Override
    public List<Product> foundByNamePage(String search, int fistnum, int lastnum) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql= "SELECT * FROM product WHERE pname like ? and pflag=0 LIMIT ?,?";
        return qr.query(sql,new BeanListHandler<Product>(Product.class),"%"+search+"%",fistnum,lastnum);
    }

    @Override
    public long foundByNameCount(String search) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select count(*) from product where pname like ? and pflag=0";
        return (Long)qr.query(sql,new ScalarHandler(),"%"+search+"%");
    }

    @Override
    public Introduce getOneProductI(String iid) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from introduce where iid = ?";
        return qr.query(sql, new BeanHandler<Introduce>(Introduce.class), iid);
    }

}
